package org.example.controllers;

import com.google.protobuf.Api;
import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.NumberCheck;
import org.example.entitys.managers.Class;
import org.example.service.inter.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/24
 */
@Controller
@Slf4j
@ResponseBody
@RequestMapping(value = "/class")
public class ClassController {
    @Autowired
    private ClassService classService;

    @PostMapping(value = "/create")
    public ApiResponse createClass(String name, String desc, int activity) {
        Class classVO = new Class(-1, name, new Date(), desc, -1, -1, activity);
        log.info("createClass(class = " + classVO + ")");
        return classService.createClass(classVO) ? ApiResponse.ok(classVO) : ApiResponse.failure();
    }

    @PostMapping("/updateTeacher")
    public ApiResponse updateTeacher(@RequestParam(value = "class", required = false) String classId, String teacher) {
        if (NumberCheck.isNotNumber(classId)) return ApiResponse.paramIncorrect("class");
        if (NumberCheck.isNotNumber(teacher)) return ApiResponse.paramIncorrect("teacher");
        log.info("updateAssistant(class_id = " + classId + ", teacher_id = " + teacher + ")");
        return classService.updateTeacher(Integer.parseInt(classId), Integer.parseInt(teacher)) ?
                ApiResponse.ok(Map.of("class_id", classId, "teacher_id", teacher)) :
                ApiResponse.failure();
    }

    @PostMapping("/updateAssistant")
    public ApiResponse updateAssistant(@RequestParam(value = "class", required = false) String classId, String assistant) {
        if (NumberCheck.isNotNumber(classId)) return ApiResponse.paramIncorrect("class");
        if (NumberCheck.isNotNumber(assistant)) return ApiResponse.paramIncorrect("assistant");
        log.info("updateAssistant(class_id = " + classId + ", assistant_id = " + assistant + ")");
        return classService.updateAssistant(Integer.parseInt(classId), Integer.parseInt(assistant)) ?
                ApiResponse.ok(Map.of("class_id", classId, "assistant_id", assistant)) :
                ApiResponse.failure();
    }

    @PostMapping("/addStudent")
    public ApiResponse insertStudentInClass(@RequestParam(value = "class", required = false) String classId, String student) {
        if (NumberCheck.isNotNumber(classId)) return ApiResponse.paramIncorrect("class");
        if (NumberCheck.isNotNumber(student)) return ApiResponse.paramIncorrect("student");
        log.info("insertStudentInClass(class = " + classId + ", student = " + student + ")");
        return classService.addStudent(Integer.parseInt(classId), Integer.parseInt(student)) ?
                ApiResponse.ok(Map.of("class_id", classId, "student_id", student)) :
                ApiResponse.failure();
    }

}
